package com.album.activity.pageView;


import java.util.Iterator;
import java.util.List;

import com.album.R;
import com.album.activity.SetstateManager;
import com.album.activity.page.Page;
import com.album.activity.page.PageManager;
import com.album.common.VerticalSeekBar;
import com.album.utils.CommonUtil;
import com.album.utils.MyLog;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PicinfoView extends Page {
	private Context context;
	private LinearLayout linearLayout; 
	private LinearLayout glinearLayout; 
	
	private Gallery gallery;
	private TextView textview02;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public PicinfoView(PageManager manager) {
		super(manager);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Object extra) {
		// TODO Auto-generated method stub
		super.onCreate(extra);
		
		//按返回跳转到CasualLookView
		SetstateManager c = new SetstateManager();
		MyLog.info(getClass(), "PicinfoView manager 1:"+manager);
		context = manager.getContext();
		LayoutInflater inflater = LayoutInflater.from(manager.getContext());
		linearLayout = (LinearLayout) inflater.inflate(
				R.layout.picpage, null);
		gallery = (Gallery) linearLayout.findViewById(R.id.gallery);
		textview02 = (TextView) linearLayout.findViewById(R.id.TextView02);
//		glinearLayout = (LinearLayout) linearLayout.findViewById(R.id.gLayout);
//		final View selectedView = ((Activity) context)
//		.getLayoutInflater().inflate(
//				R.layout.pic_bright, null);
		//长按事件  出现调整亮度功能
		gallery.setOnItemLongClickListener(new OnItemLongClickListener() {  
			  
	        @Override  
	        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,  
	                int arg2, long arg3) {  
	            // TODO Auto-generated method stub  
	        	try {
	        		 MyLog.info(getClass(), "gallery长按事件"); 
	 	           // glinearLayout.addView(selectedView);
				} catch (Exception e) {
					e.printStackTrace();
				}
	            return false;  
	        }  
	      });  
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MyLog.info(getClass(), "gallery点击事件"); 
	           // glinearLayout.removeView(selectedView);
			}
			
		});
		gallery.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				MyLog.info(getClass(), "gallery滑动事件"); 
				textview02.setText(1+"/"+SetstateManager.piclist.size());
				return false;
			}
		});
		textview02.setText(1+"/"+SetstateManager.piclist.size());
		final ImageAdapter adapter = new ImageAdapter(context,SetstateManager.piclist);
		gallery.setAdapter(adapter);
		gallery.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				MyLog.info(getClass(), "gallery滑动事件"); 
				//textview02.setText(adapter.getItemId(0)+"/"+SetstateManager.piclist.size());
				//adapter.notifyDataSetChanged();
				return false;
			}
		});
		//final VerticalSeekBar sb = (VerticalSeekBar) selectedView.findViewById(R.id.SeekBar01);  
		// 设置拖动条的初始值和文本框的初始值  
		
     //设置拖动条改变监听器  
//		seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//		
//		@Override
//		public void onStopTrackingTouch(SeekBar seekBar) {
//			// TODO Auto-generated method stub
//			
//		}
//		
//		@Override
//		public void onStartTrackingTouch(SeekBar seekBar) {
//			// TODO Auto-generated method stub
//			
//		}
//		
//		@Override
//		public void onProgressChanged(SeekBar seekBar, int progress,
//				boolean fromUser) {
//			MyLog.info(getClass(), "亮度改变");
//			
//		}
//	});
	}

	//	@Override
//	public void onCreateContextMenu(ContextMenu menu, View v,
//			ContextMenuInfo menuInfo) {
//		menu.add("Action");
//	}
//
//	@Override
//	public boolean onContextItemSelected(MenuItem item) {
//		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
//				.getMenuInfo();
//		Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
//				.show();
//		return true;
//	}
	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context mContext;
		private List<Integer> picList;
		private int position;


		public ImageAdapter(Context context,List<Integer> picList) {
			mContext = context;
            this.picList = picList;
			TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1);
			mGalleryItemBackground = a.getResourceId(
					R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}

		@Override
		public int getCount() {
			return picList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return this.position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			this.position = position;
			ImageView i = new ImageView(mContext);
			i.setImageResource(picList.get(position));
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			//i.setLayoutParams(new Gallery.LayoutParams(CommonUtil.width, LayoutParams.WRAP_CONTENT));

			// The preferred Gallery item background
			//i.setBackgroundResource(mGalleryItemBackground);
			
			return i;
		}
		

	}
	@Override
	public View getView() {
		// TODO Auto-generated method stub
		return linearLayout;
	}
}
